package com.china08.yxyapi.api.manage.sch.platform.live.setting;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class LiveSchLessonSettingListController extends YxyApiBaseController {

    @Autowired
    JdbcTemplate JdbcTemplate;

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @Autowired
    private YxyLiveCatalogRepository catalogRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/live/lesson/setting/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        String catalogId3 = reqModel.catalogId3;
        Integer level = reqModel.level;
        String keyword = (StringUtils.isNotBlank(reqModel.keyword)) ? "%" + reqModel.keyword + "%" : "";
        String schoolId = getProfile(request, response).getSchoolId();
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");

        StringBuffer sb = new StringBuffer();

        sb.append(" SELECT                          ");
        sb.append("   t2.id                            ");
        sb.append(" FROM                            ");
        sb.append("   yxy_live.yxy_live_lesson t1     ");
        sb.append(" LEFT JOIN yxy_live.yxy_live_lesson_item t2                      ");
        sb.append(" ON t1.id = t2.lesson_id  ");
        sb.append(" WHERE t2.record_open = 0 ");
        sb.append(" AND t1.creater_user_school_id= '" + schoolId + "'");
        List<Object> paramList = new ArrayList<>();

        if (level == 1) {
            if (StringUtils.isNotBlank(catalogId1)) {
                sb.append(" AND t1.catalog_id1 = ? ");
                paramList.add(catalogId1);
            }
        }
        if (level == 2) {
            if (StringUtils.isNotBlank(catalogId2)) {
                sb.append(" AND t1.catalog_id2 = ? ");
                paramList.add(catalogId2);
            }
        }
        if (level == 3) {
            if (StringUtils.isNotBlank(catalogId3)) {
                sb.append(" AND t1.catalog_id3 = ? ");
                paramList.add(catalogId3);
            }
        }
        if (StringUtils.isNotBlank(reqModel.keyword)) {
            sb.append("   AND t1.name like ?                                ");
            paramList.add(keyword);
        }

        List<String> countList = JdbcTemplate.queryForList(sb.toString(), paramList.toArray(), String.class);

        sb.append("  ORDER BY t2.end_date  DESC                             ");

        sb.append(" limit " + pageable.getOffset() + "," + pageable.getPageSize());
        List<String> idList = JdbcTemplate.queryForList(sb.toString(), paramList.toArray(), String.class);

        List<RespModel> respModels = new ArrayList<>();

        for (String string : idList) {
            RespModel model = new RespModel();

            String cId1 = catalogId1;
            String cId2 = catalogId2;
            String cId3 = catalogId3;
            YxyLiveLessonItem yxyLiveLessonItem = yxyLiveLessonItemRepository.findById(string);

            String lessonId = yxyLiveLessonItem.getLessonId();
            YxyLiveLesson yxyLiveLesson = yxyLiveLessonRepository.findById(lessonId);

            if (StringUtils.isNotBlank(cId1)) {
                YxyLiveCatalog findById = catalogRepository.findById(cId1);
                model.catalog1Id = cId1;
                model.catalog1Name = findById.getName();
            }
            if (StringUtils.isNotBlank(cId2)) {
                YxyLiveCatalog findById = catalogRepository.findById(cId2);
                model.catalog2Id = cId2;
                model.catalog2Name = findById.getName();
            }
            if (StringUtils.isNotBlank(cId3)) {
                YxyLiveCatalog findById = catalogRepository.findById(cId3);
                model.catalog3Id = cId3;
                model.catalog3Name = findById.getName();
            }

            model.count = yxyLiveLesson.getCount();
            model.createDate = DateToolUtils.format(yxyLiveLesson.getCreateDate(), "yyyy-MM-dd");
            model.bTime = DateToolUtils.format(yxyLiveLessonItem.getBeginDate(), "yyyy-MM-dd");
            model.eTime = DateToolUtils.format(yxyLiveLessonItem.getEndDate(), "yyyy-MM-dd");
            model.name = yxyLiveLesson.getName();
            model.liveType = yxyLiveLesson.getLiveType() == 0 ? "直播+录播" : "直播";
            model.schoolName = yxyLiveLesson.getTeacherSchoolNick();
            model.status = "待审核";
            model.user = yxyLiveLesson.getTeacherNick();
            model.userId = yxyLiveLesson.getTeacherId();
            model.cover = yxyLiveLesson.getCover();
            model.id = yxyLiveLesson.getId();
            model.ep = yxyLiveLesson.getEp();
            model.epTimes = yxyLiveLesson.getEpTimes();
            model.pv = yxyLiveLesson.getPv();

            respModels.add(model);
        }

        Page<RespModel> page = new PageImpl<>(respModels, pageable, countList.size());
        return page;
    }

    public static class ReqModel extends YxyApiPageReqModel {

        public Integer level;
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String keyword;

    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
        public String createDate;
        public String name;
        public String status;
        public Integer count;
        public String cover;
        public String catalog1Name;
        public String catalog1Id;
        public String catalog2Name;
        public String catalog2Id;
        public String catalog3Name;
        public String catalog3Id;
        public String user;
        public String userId;
        public String schoolName;
        public String liveType;
        public Integer pv;// 观看次数
        public Float ep;// 评分
        public Integer epTimes;// 评分次数
        public String bTime;
        public String eTime;

    }

}
